package com.cts.controller.admin;

import com.cts.annotation.OperationLogger;
import com.cts.model.dto.LogDTO;
import com.cts.model.vo.PageResultVO;
import com.cts.model.vo.Result;
import com.cts.service.OperationLogService;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @projectName: CBlog
 * @package: com.cts.controller.admin
 * @className: OperationLogController
 * @author: chentongsheng
 * @description: TODO
 * @date: 2023/4/5 12:06
 * @version: 1.0
 */
@RequestMapping("/admin")
@RestController
public class OperationLogController {

    @Autowired
    OperationLogService operationLogService;

    @OperationLogger("获取日志列表")
    @ApiOperation("获取日志列表")
    @PostMapping("getOperationLogList")
    public Result getOperationLogList(@RequestBody LogDTO logDTO) {
        if(StringUtils.isNullOrEmpty(logDTO.getBeginTime()) && !StringUtils.isNullOrEmpty((logDTO.getEndTime()))) {
            return Result.error("开始日期为空但是结束日期不为空");
        }
        if(!StringUtils.isNullOrEmpty(logDTO.getBeginTime()) && StringUtils.isNullOrEmpty((logDTO.getEndTime()))) {
            return Result.error("开始日期不为空但是结束日期为空");
        }
        if(!StringUtils.isNullOrEmpty(logDTO.getBeginTime()) && !StringUtils.isNullOrEmpty(logDTO.getEndTime())
                && (logDTO.getBeginTime().compareTo(logDTO.getEndTime()) == 1)) {
            return Result.error("开始日期大于结束日期");
        }
        PageResultVO operationVo = operationLogService.getOperationLogList(logDTO);
        return Result.ok("请求成功", operationVo);
    }

    @OperationLogger("删除日志")
    @ApiOperation("删除日志")
    @PostMapping("deleteOperationLog")
    public Result deleteOperationLog(Long id) {
        operationLogService.removeById(id);
        return Result.ok("请求成功");
    }
}
